package com.dawnling.greendaodemo.app;

import android.app.Application;
import android.database.sqlite.SQLiteDatabase;

import com.dawnling.greendaodemo.converter.MySQLiteOpenHelper;
import com.dawnling.greendaodemo.greendao.gen.DaoMaster;
import com.dawnling.greendaodemo.greendao.gen.DaoSession;
import com.github.yuweiguocn.library.greendao.MigrationHelper;

public class MyApplication extends Application {

    public static MyApplication instance;
    private DaoSession daoSession;
    private SQLiteDatabase db;

    public static synchronized MyApplication getInstance() {
        return instance;
    }

    @Override
    public void onCreate() {
        super.onCreate();
        instance = this;
        initDataBase();
    }

    private void initDataBase() {
//        // regular SQLite database //这里之后会修改，关于升级数据库
//        helper = new DaoMaster.DevOpenHelper(this, "notes-db");
//        db = helper.getWritableDatabase();
//        // encrypted SQLCipher database
//        // note: you need to add SQLCipher to your dependencies, check the build.gradle file
//        // DaoMaster.DevOpenHelper helper = new DaoMaster.DevOpenHelper(this, "notes-db-encrypted");
//        // Database db = helper.getEncryptedWritableDb("encryption-key");
//        daoSession = new DaoMaster(db).newSession();
        //这里之后会修改，关于升级数据库
        MigrationHelper.DEBUG = true; //如果你想查看日志信息，请将DEBUG设置为true
        MySQLiteOpenHelper helper = new MySQLiteOpenHelper(this, "notes-db",
                null);
        db = helper.getWritableDatabase();
        daoSession = new DaoMaster(helper.getWritableDatabase()).newSession();
    }

    public DaoSession getDaoSession() {
        return daoSession;
    }

    public SQLiteDatabase getDb(){
        return db;
    }
}
